package action;

import java.util.Map;

import javax.servlet.http.*;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import static util.ConstantStringTable.*;

/**
 * 
 * Process the logout action.<br>
 * 
 * Clear the cookie and remove the user object in the session.
 * 
 * @author Cylixstar
 *
 */
@SuppressWarnings("serial")
@Action("logout")
@Result(name="success",type="redirect",location="/index.jsp")
public class LogoutAction extends ActionSupport {

	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request= ServletActionContext.getRequest();
		Map session = ActionContext.getContext().getSession();
		
		session.remove(LOGIN_USER);
		Cookie cks[] = request.getCookies();
		if(cks!=null){
			for(Cookie ck:cks){
				if(ck.getName().equals(LOGIN_COOKIE)){
					ck.setMaxAge(0);
					response.addCookie(ck);
				}
			}
		}
		
		return SUCCESS;
	}
	
}
